﻿<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
       "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
<meta http-equiv="content-type" content="text/html; charset=utf-8">
</head>
<body>



<?php
ini_set('display_errors', '1');
error_reporting(E_ALL);
include("inc/joomlaInt.php");
include("inc/inc_configuration.php");

if ($jUser==0)
{
	echo "Bitte zuerst einloggen!";
}
else
{
	$uId=$_GET["id"];
	
	$q="SELECT id, name, username, registerdate, lastvisitdate FROM $tbl_users WHERE id=$uId";
	$qe=mysql_query($q);
	$qeo=mysql_fetch_object($qe);
	
	$name=$qeo->name;
	$username=$qeo->username;
	$registerdate=$qeo->registerdate;
	$lastvisitdate=$qeo->lastvisitdate;
	
	//TODO AUSGABE
	
	$q="SELECT * FROM $tbl_ui WHERE userId=$uId";
	$qe=mysql_query($q);
	$qc=mysql_num_rows($qe);
	
	if ($qc>0)
	{
		$qeo=mysql_fetch_object($qe);
		$gamertag=$qeo->gamertag;
		$icq=$qeo->icq;	
	}
	else
	{
		$gamertag="Noch nicht angegeben";
		$icq="Noch nicht angegeben";
	}
	
	$edit="";
	
	if ($jUser==$uId)
	{
		$edit="<a href='profil_bearbeiten.php?id=$jUser'>Profil bearbeiten</a>";
	}
	
	//Avatar
	
	$q="SELECT avatar FROM $tbl_avatar WHERE userId=$uId";
	$qe=mysql_query($q);
	$qc=mysql_num_rows($qe);
	
	if ($qc>0)
	{
		$qeo=mysql_fetch_object($qe);
		$avatar=$qeo->avatar;
		
		$avimg=$avatarpath.$avatar;
	}
	else
	{
		$avimg="img/keinfoto.jpg";
	}
	
	
	//		ERFOLGE
	
	$eq="	SELECT 
				c.teamId,
				e.playerId,
				e.bezeichnung,
				s.number,
				t.name,
				l.name AS liga
	
			FROM 	$tbl_erfolge e, 
					$tbl_combinations c, 
					$tbl_season s, 
					$tbl_leagues l,
					$tbl_teams t
			WHERE 
				e.combinationId=c.id AND 
				c.userId = $uId AND
				e.leagueId = l.id AND
				l.seasonId = s.id AND
				c.teamId = t.id
				
			ORDER BY leagueId";
			
			
	$eQe = mysql_query($eq);
	$eanz = mySQL_num_rows($eQe);
	
	$e = "";
	$erfolge="";
	
	if ($eanz>0)
	{
		while ($row = mysql_fetch_object($eQe))
		{
			$team = $row->name;
			$playerId = $row->playerId;
			$bez = $row->bezeichnung;
			$num = $row->number;
			$liga = $row->liga;
			$teamId = $row->teamId;
			
			if ($playerId==0)
			{
				$e.="	<tr>
							<td><b>$num. Saison</b></td>
							<td>$liga</td>
							<td>$bez</td>
							<td>mit</td>
							<td>$team</td>
							<td><img src='img/logos/$teamId.png' width='35' height='35'></td>
						</tr>";
			}
			else
			{
				$pq="SELECT firstname, name FROM $tbl_players WHERE id = $playerId";
				$pqe=mysql_query($pq);
				$pqeo=mysql_fetch_object($pqe);
				$plname=$pqeo->name;
				$fname=$pqeo->firstname;
				
				$playern="$fname $plname";
				
				$e.="	<tr>
							<td><b>$num. Saison</b></td>
							<td>$liga</td>
							<td>$bez</td>
							<td>mit</td>
							<td>$playern</td>
							<td><img src='img/logos/$teamId.png' width='35' height='35'></td>
						</tr>";
			
			}
		

		}
	
		$erfolge = "	<table id='tabelle'>
				<tr>
					<thead>
						<th colspan='6'>Erfolge</th>
					</thead>
				</tr>
				$e
				<tr>
					<thead>
						<th colspan='6'></th>
					</thead>
				</tr>
			</table>";

	
	
	
	}
	
	
	
	$cQ = "SELECT teamId,name FROM $tbl_combinations c, $tbl_teams t WHERE c.userId=$uId AND c.active=1 AND c.teamId=t.id";
	$cQe = mysql_query($cQ);
	$canz = mySQL_num_rows($cQe);
	
	if ($canz>0)
	{
		$t1nO=mysql_fetch_object($cQe);
		
		
		$t1n=$t1nO->name;
		$t1i=$t1nO->teamId;
		
		$teams="<a class='tooltip'><img src='img/logos/$t1i.png' width='35' height='35'><span>$t1n</span></a>";
		
		if ($canz==2)
		{
			$t2nO=mysql_fetch_object($cQe);
			$t2n=$t2nO->name;
			$t2i=$t2nO->teamId;
			
			$teams.= "<a class='tooltip'><img src='img/logos/$t2i.png' width='35' height='35'><span>$t2n</span></a>";
		}
	}
	
	$q="	SELECT
				m.id AS mId,
				m.homeT,
				m.guestT,
				m.homeG,
				m.guestG,
				m.free,
				m.homeC,
				m.guestC,
				m.playDate,
				m.leagueId
			FROM
				$tbl_matches m,
				$tbl_combinations c
			WHERE
				m.free=3 AND
				c.userId=$uId AND
				(m.homeC=c.id OR m.guestC=c.id)
			ORDER BY 
				confDate DESC
			LIMIT 0,5";
	
	
	$qe=mysql_query($q);

	$letztespiele="";
	
	$scount=mysql_num_rows($qe);
	
	if ($scount>0)
	{
		while ($row = mysql_fetch_object($qe))
		{
			$mI=$row->mId;
			$homeT=$row->homeT;
			$guestT=$row->guestT;
			$hG=$row->homeG;
			$gG=$row->guestG;
			$f=$row->free;
			$hC=$row->homeC;
			$gC=$row->guestC;
			$liga=$row->leagueId;
			$playdate=$row->playDate;
			
			
			$datum=date("d.m.y <br> H:i",$playdate);
			
			$lpq="SELECT lp.path AS path FROM $tbl_leagues l, $tbl_leaguepics lp WHERE l.id=$liga AND l.picId=lp.id";
			$lpqe=mysql_query($lpq);
			$lpqeo=mysql_fetch_object($lpqe);
			$path=$lpqeo->path;

			
			$b="<a href='spielbericht.php?matchID=$mI&type=2'><img src='img/bericht.png' alt='Spielbericht'></a>";
			
			
			
			$tHq="SELECT name FROM $tbl_teams t WHERE t.id=$homeT";
			$tGq="SELECT name FROM $tbl_teams t WHERE t.id=$guestT";
			$tHqe = mysql_query($tHq);
			$tGqe = mysql_query($tGq);
			$tHn = mysql_fetch_object($tHqe)->name;
			$tGn = mysql_fetch_object($tGqe)->name;
			
			
			$uHSql="	SELECT u.username AS uHn, c.id AS hC, u.id AS uHi FROM $tbl_combinations c, $tbl_users u
						WHERE
						(c.id=$hC) AND c.userId=u.id";
			$uGSql="	SELECT u.username AS uGn, c.id AS gC, u.id AS uGi FROM $tbl_combinations c, $tbl_users u
						WHERE
						(c.id=$gC) AND c.userId=u.id";
			
			$uHq = mysql_query($uHSql);
			$uGq = mysql_query($uGSql);
			
			$uHo = mysql_fetch_object($uHq);
			$uGo = mysql_fetch_object($uGq);
			
			$uHn = $uHo->uHn;
			$uGn = $uGo->uGn;
			
			
			
			if ($uHn=="")
			{
				$uHn = "(-)";
			}
			
			if ($uGn=="")
			{
				$uGn = "(-)";
			}
			
			$uHi=$uHo->uHi;
			$uGi=$uGo->uGi;
			

			$letztespiele .= "	<tr>
									<td><img src='$leaguelogo_s_path$path' width='50' height='50' alt='Kein Foto!'></td>
									<td width='42%'><a href='spiele_team.php?team=$homeT&liga=$liga'>$tHn</a><br><a href='profil.php?id=$uHi'>$uHn</a></td>
									<td><a href='team_kader.php?team=$homeT'><img src='img/logos/$homeT.png' width='35' height='35' alt=''></a></td>
									<td width='3%'><center>$hG</center></td>
									<td width='2%'><center>:</center></td>
									<td width='3%'><center>$gG</center></td>
									<td><a href='team_kader.php?team=$guestT'><img src='img/logos/$guestT.png' width='35' height='35' alt=''></a></td>
									<td width='42%'><center><a href='spiele_team.php?team=$guestT&liga=$liga'>$tGn
									<br>
									<a href='profil.php?id=$uGi'>$uGn</center></td>
									<td width='4%'><center>$datum</center></td>
									<td width='4%'><center>$b</center></td>
								</tr>";
		}
	}	
	
	echo "	<h1>$username</h1>
			<table>
				<tr>
					<td>
						<table id='tabelle'>
							<thead>
								<tr>
									<th></th>
									<th></th>
								</tr>
							</thead>
							<tr>
								<td>Username</td>
								<td>$username</td>
							</tr>
							<tr>
								<td>Name</td>
								<td>$name</td>
							</tr>
							<tr>
								<td>Teams</td>
								<td>$teams</td>
							</tr>
							<tr>
								<td>Registrierungsdatum</td>
								<td>$registerdate</td>
							</tr>
							<tr>
								<td>Zuletzt online</td>
								<td>$lastvisitdate</td>
							</tr>
							<tr>
								<td>Gamertag</td>
								<td>$gamertag</td>
							</tr>
							<tr>
								<td>ICQ</td>
								<td>$icq</td>
							</tr>
							<thead>
								<tr>
									<th colspan='2'>$edit</th>
								</tr>
							</thead>
						</table>
					</td>
					<td>
						<img src='",$avimg,"' alt='FOTO!'>
					</td>
				</tr>";


	echo "<br>$erfolge<br>";
				
					
	echo "<table id='tabelle'>
				<tr>
					<thead>
						<th colspan='10'>Letzte Spiele</th>
					</thead>
				</tr>
				$letztespiele
				<tr>
					<thead>
						<th colspan='10'></th>
					</thead>
				</tr>
				</table>";
	
	$abfrage = "SELECT c.id,t.name,t.id AS tid, t.rating,c.joinDate,c.lastMatchDate AS lmd, c.active FROM $tbl_combinations c, $tbl_teams t WHERE c.userId=$uId
				AND c.teamId=t.id
				ORDER BY
					active DESC, joinDate DESC";
	$ergebnis = mysql_query($abfrage);
	
	
	echo "	<br><br>
			<table id='tabelle'>
			<thead>
				<tr>
					<th colspan='7'>Gesamt</th>
					<th colspan='6'>Heim</th>
					<th colspan='6'>Auswärts</th>
					<th colspan='2'></th>
				</tr>
				<tr>
					<th>Team</th>
					<th>SP</th>
					<th>S</th>
					<th>U</th>
					<th>N</th>
					<th>T:GT</th>
					<th>DS</th>
					
					<th>SP</th>
					<th>S</th>
					<th>U</th>
					<th>N</th>
					<th>T:GT</th>
					<th>DS</th>
					
					<th>SP</th>
					<th>S</th>
					<th>U</th>
					<th>N</th>
					<th>T:GT</th>
					<th>DS</th>
					
					<th>Amtsantritt</th>
					<th>Letztes Spiel</th>
				</tr>
			</thead>";
	while ($row=mysql_fetch_object($ergebnis))
	{
		$tid=$row->tid;
		$tname=$row->name;
		$cid=$row->id;
		$ac=$row->active;
		
		if ($ac==0)
		{
			$class="inactive";
		}
		else
		{
			$class="active";
		}
		
		$q="SELECT * FROM $tbl_matches WHERE free=3 AND ((homeC=$cid AND (homeT=$tid)) OR (guestC=$cid AND(guestT=$tid)))";
		$qe=mysql_query($q);
		
		$uh=0;
		$ug=0;
		$sh=0;
		$sg=0;
		$nh=0;
		$ng=0;
		$gh=0;
		$gg=0;
		$gth=0;
		$gtg=0;
		$spges=0;
		$tges=0;
		$gtges=0;
		$sges=0;
		$uges=0;
		$nges=0;
		$phs=0;
		$pgs=0;
		$pgess=0;
		
		while ($match=mysql_fetch_object($qe))
		{
			$homeT=$match->homeT;
			$guestT=$match->guestT;
			$hG=$match->homeG;
			$gG=$match->guestG;
			$winner=$match->winner;
			
			
			
			if ($homeT==$tid)
			{
				$gh=$gh+$hG;
				$gth=$gth+$gG;
				
				switch ($winner) {
					case 1:
						$sh=$sh+1;
						break;
					case 2:
						$nh=$nh+1;
						break;
					case 3:
						$uh=$uh+1;
						break;
				}
			}
			else
			{
				$gg=$gg+$gG;
				$gtg=$gtg+$hG;
				
				switch ($winner) {
					case 1:
						$ng=$ng+1;
						break;
					case 2:
						$sg=$sg+1;
						break;
					case 3:
						$ug=$ug+1;
						break;
				}
			}			
		}
		
		$spges=$uh+$ug+$sh+$sg+$nh+$ng;
		$tges=$gh+$gg;
		$gtges=$gth+$gtg;
		$sges=$sh+$sg;
		$uges=$uh+$ug;
		$nges=$nh+$ng;
		
		$sph=$uh+$sh+$nh;
		$spg=$ug+$sg+$ng;
		
		$ph=$uh+$sh*3;
		$pg=$ug+$sg*3;
		$pges=$ph+$pg;
		
		$phs=number_format(round($ph/$sph,2),2, ',', '');
		$pgs=number_format(round($pg/$spg,2),2, ',', '');
		$pgess=number_format(round($pges/$spges,2),2, ',', '');
		
		$jdd = $row->joinDate;
		$jd = date("d.m.y",$jdd);
		
		$lmd=$row->lmd;
		
		
		if ($lmd=="")
		{
			$ld=" - ";
		}
		else
		{
			//$ld = date("d.m.Y - H:i",$lmd);
			$ld = date("d.m.y",$lmd);
		}
		
		echo "	<tr class='",$class,"'>
					<td><a class='tooltip'><img src='img/logos/$tid.png' width='35' height='35'><span>$tname</span></a></td>
					<td>$spges</td>
					<td>$sges</td>
					<td>$uges</td>
					<td>$nges</td>
					<td>$tges:$gtges</td>
					<td>$pgess</td>

					
					<td>$sph</td>
					<td>$sh</td>
					<td>$uh</td>
					<td>$nh</td>
					<td>$gh:$gth</td>
					<td>$phs</td>
					
					<td>$spg</td>
					<td>$sg</td>
					<td>$ug</td>
					<td>$ng</td>
					<td>$gg:$gtg</td>
					<td>$pgs</td>
					
					<td>$jd</td>
					<td>$ld</td>
				</tr>";
		
	}

	
	echo '<thead><tr><th colspan="21"></th></tr></thead></table>';
	echo'<center><br><br><a href="javascript:window.history.back()" class="button zurueck">Zurück</a></center>';
}
?>

</body>
</html>